/**
 * 功能权限
 */
var viewflag = true;
mini.parse();
var tree = mini.get("tree1");
var datagrid1 = mini.get("datagrid1");

var grid = mini.get("grid1");

var roleId;
var nodeselect = '';
var nodeId = '';
var menuId = '';
var datalist = [];// 总数据
init();

function init() {

	mini.get("sp").set({
		height : document.body.clientHeight - 40
	});
	setInitMask();
	// 加载角色
	$.ajax({
		type : 'POST',
		url : ajaxUrl + "/role/list.do",
		dataType : 'json',
		contentType : "application/json",
		success : function(data) {
			// console.log(data)
			datagrid1.setData(data.data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			alert('获取失败！');
		}
	})
}

function onRoleChanged(e) {
	// console.log(e.selected.roleId);
	roleId = e.selected.roleId;

	// 加载左侧树
	tree.loading();
	tree.load(ajaxUrl + "/roleMath/findAllAuth.do?roleId=" + roleId);
	console.log(tree.getList());

	// 保存上一个菜单的表格数据
	tree.on("beforenodeselect", function(e) {

		savePre();
	});

	// 显示表格，设置选中
	tree.on("nodeselect", function(e) {
		console.log(e.selected);
		nodeselect = e.selected;
		menuId = e.selected.menuId;
		// grid.loading();
		// 加载右侧表格
		$.ajax({
			type : 'POST',
			// url: "http://192.168.31.65:8080/bpm/formltems/findByFormListId.do?formId=" + e.selected.formId,
			url: ajaxUrl+"/formltems/findByFormListId.do?formId=" + e.selected.formMenuId,
			dataType : 'json',
			//contentType: "application/json",
			success: function (data) {

				console.log(data);
				var selects;
				var rows = [];
				var checkFlag = false;

				grid.setData(data.data);
				if(data.data.length == 0) {
					mini.showTips({
						content: "暂无数据",
						state: 'success',
						x: 'Center',
						y: 'Center',
						timeout: 2000
					});
				}else {
					//设置选中
					for (var k = 0; k < datalist.length; k++) {
						if (menuId == datalist[k].menus) {// 修改过
							checkFlag = true;
							selects = datalist[k].roleDataList.split(',');
						}
					}
					if (!checkFlag) {// 未修改过
						selects = [];
					}

					//here
					for (var i = 0; i < data.data.length; i++) {
						for (var j = 0; j < selects.length; j++) {
							if (data.data[i].cTableField == selects[j]) {
								rows.push(data.data[i]);
							}
						}
					}
					grid.selects(rows);
				}

			},
			error: function (jqXHR, textStatus, errorThrown) {
				alert('获取失败！');
			}
		})
	});

	setInitMask();
}

// 编辑
function update() {
	mini.get("but_update").disable();
	mini.get("but_save").enable();
	mini.get("but_abandon").enable();
	viewflag = false;
	tree.unmask();
	grid.unmask();
	datagrid1.mask({
		html : "<div></div>",
		cls : "mini-panel",
		style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
	});

}

// 放弃
function abandon() {
	mini.get("but_update").enable();
	mini.get("but_save").disable();
	mini.get("but_abandon").disable();
	setInitMask();
}

// 保存
function save() {

	savePre();
	console.log(datalist);
	$.ajax({
		// url : "http://192.168.31.65:8080/bpm/roleMathData/insertRoleMathData.do",
		url : ajaxUrl + "/roleMathData/insertRoleMathData.do",
		type : "POST",
		async : false,
		contentType : "application/json",
		data : JSON.stringify(datalist),
		success : function(data) {
			// abandon();
			
			mini.alert("保存成功！")
		},
		error : function(data) {
			mini.alert("保存失败！")
		}
	});
}

function setInitMask() {
	tree
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});
	grid
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});
	datagrid1.unmask();
}

function savePre() {
	console.log(datalist, 'datalist');
	var isSelectd = false;// 是否被选过
	var selects = '';//被选中的数据串
	var nodes = grid.getSelecteds();
	for (var i = 0; i < nodes.length; i++) {
		selects += nodes[i].cTableField + ','
	}
	selects = selects.substring(0,selects.length-1);//去掉逗号
	
	// 存数据
	for (var p = 0; p < datalist.length; p++) {
		if (datalist[p].menus == menuId) {// 数据已存在，修改
			isSelectd = true;
			datalist[p].roleDataList = selects;
		}
	}
	if (!isSelectd && nodeselect != '') {// 数据不存在，push
		datalist.push({
			roleId : roleId,
			roleDataList : selects,
			menuId : menuId
		})
	}
	console.log(datalist, 'datalist已保存');
}